package com.isoftstone.guodian.purApplication.control;

import com.isoftstone.guodian.demo.service.TFultbpurchasingapplyService;
import com.isoftstone.guodian.entity.TFultbpurchasingapplyEntity;
import com.isoftstone.guodian.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class purApplicationContr {

    @Autowired
    TFultbpurchasingapplyService tFultbpurchasingapplyService;


    @RequestMapping("/detailTFultbpurchasingapply")
    public String dateilTFultbpurchasingapply(String id, Model model) throws Exception {
        TFultbpurchasingapplyEntity entity = tFultbpurchasingapplyService.selectById(id);
        model.addAttribute("entity", entity);
        return "/demo/detail_cgjhsq.jsp";
    }

    @RequestMapping("/selectTFultbpurchasingapply")
    public String selectTFultbpurchasingapply(Integer pageNow,Model model) {

        TFultbpurchasingapplyEntity entity=new TFultbpurchasingapplyEntity();
        try {
            int paget = tFultbpurchasingapplyService.selectTotal(null);
            PageUtils page = new PageUtils();
            if (pageNow == null) {
                pageNow = 1;
            }
            if (pageNow < 1) {
                pageNow = 1;
            } else if (pageNow > paget) {
                pageNow = paget;
            }
            page.setPageNow(pageNow);
            page.setTotal(paget);
            entity.setPageUtils(page);
            List<TFultbpurchasingapplyEntity> list = tFultbpurchasingapplyService.selectForPage(entity);
            model.addAttribute("rows",list);
            model.addAttribute("page", page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "demo/cgjhsq.jsp";
    }

    @RequestMapping("/insertTFultbpurchasingapply")
    public String insertTFultbpurchasingapply(TFultbpurchasingapplyEntity entity, Model model,@RequestParam(required = false)String id) throws Exception {
        entity.setCreatedate(entity.getWritedate());
        entity.setJhtimeEnd(entity.getWritedate());
        if (entity.getApplystate() == null) {
            entity.setApplystate("审批中");
        }
        if (entity.getExecutestate() == null) {
            entity.setExecutestate("已提交");
        }
        if (id != null&&"1".equals(id)) {
            entity.setApplystate("草稿");
        } else if ("2".equals(id)) {
            tFultbpurchasingapplyService.updateEntity(entity);
            return "redirect:selectTFultbpurchasingapply";
        }
        String result;
        if(tFultbpurchasingapplyService.saveEntity(entity)==1)
        {
            result = "success";
        } else {
            result = "failed";
        }
        model.addAttribute("result", result);
        return "redirect:selectTFultbpurchasingapply";
    }

    @RequestMapping("/updateTFultbpurchasingapply")
    public String updateTFultbpurchasingapply(TFultbpurchasingapplyEntity entity,Model model) throws Exception {
        if(tFultbpurchasingapplyService.updateEntity(entity)==1)
        {
            model.addAttribute("result", "success");
        } else {
            model.addAttribute("result", "failed");
        }
        return "redirect:selectTFultbpurchasingapply";
    }

    @RequestMapping("/deleteTFultbpurchasingapply")
    public String deleteTFultbpurchasingapply(String id, Model model) throws Exception {
        if (tFultbpurchasingapplyService.deleteEntity(id) == 1) {
            model.addAttribute("result", "success");
        } else {
            model.addAttribute("result", "failed");
        }
        return "redirect:selectTFultbpurchasingapply";
    }

    @RequestMapping("/gaibian")
    public String shenpi(int n,String id) throws Exception {
        TFultbpurchasingapplyEntity entity = tFultbpurchasingapplyService.selectById(id);
        String gaibian = entity.getApplystate();
        switch (n) {
            case 1:
                gaibian = "已发布";
                break;
            case 2:
                gaibian = "已驳回";
                break;
        }
        entity.setApplystate(gaibian);
        tFultbpurchasingapplyService.updateEntity(entity);
        return "redirect:selectTFultbpurchasingapply";
    }


    @RequestMapping("/update")
    public String update(String id, Model model) throws Exception {
        TFultbpurchasingapplyEntity entity = tFultbpurchasingapplyService.selectById(id);
        model.addAttribute("entity", entity);
        return "demo/update.jsp";
    }
}
